<?php
namespace Addons\Wechat\Controller;

class ApiController extends WechatController {
	public function _initialize() {
		parent::_initialize();

	}

	/*****微信操作接口*******/
	public function index() {
		/******************接受信息操作******************************/
		$this->wechatObj->getMsg();
		$Content = trim($this->wechatObj->postObj->Content);
		$Event = trim($this->wechatObj->postObj->Event);
		$openid = trim($this->wechatObj->postObj->FromUserName);
		$keyName = trim($this->wechatObj->postObj->EventKey);
		$MsgType = trim($this->wechatObj->postObj->MsgType);
		$MediaId = trim($this->wechatObj->postObj->MediaId);
		$postObj = $this->wechatObj->postObj;
		/***用户消息信息***/

		//$this->wechatObj->makeText($Content);
		/********关键字回复***/
		$store_id = $this->storeInfo['store_id'];
		$this->model = BoxModel("addon_wx_search");
		$this->model2 = BoxModel("addon_wx_title");
		$Page = $this->setPager(100, 5);
		//dump($Page);
		if ($this->k = $Content) {
			$k = intval($this->k);
			if ($k > 0) {
				/******回复数字****/
				$k = number2Chinese($k);
				$k = "第{$k}期";
				$sql .= " store_id='$store_id'  and (rule_head = '$k' or rule_head = '$this->k')";
				$this->data = $this->model->where($sql)->find();
				$rule_reply = $this->data['rule_reply'];
				/*****没有找到相关期数***/
				if (!$this->data) {
					$one = $this->model->where("store_id='$store_id'")->order("addtime desc")->find();
					if ($one['rule_head']) {
						$rule_reply = "你好，帖子已经更新至" . $one['rule_head'];
					}
				}
				/******回复数字结束****/
			} else if (checkstr($this->k, "搜")) {
				/******回复带搜****/
				$k = str_replace("搜", "", $this->k);
				/*****页数**/
				$p = findNum($k);
				if (intval($p) > 0) {
					$_GET['p'] = $p;
					$k = str_replace($p, "", $k);
				}
				$sql = "   store_id='$store_id' and (content like '%$k%') and  topic_id>0";
				$count = $this->model2->where($sql)->count();
				$Page = $this->setPager($count, 5);
				$order = "id desc";
				$this->data = $this->model2->where($sql)->limit($this->limit)->order($order)->select();
				$rule_reply = "";
				foreach ($this->data as $one) {
					$rule_reply .= $one['content'] . "\n\n\n";
				}
				/***没有数据**/
				if (!$this->data) {
					$rule_reply = "亲爱的站友，与{$k}相关的帖子数为0，建议您更换关键词哦";
				}
				/****超过5条**/
				if ($count > 5) {
					$next_page = $Page->nowPage + 1;
					$rule_reply = "与“{$Content}”有关的帖子共{$count}条，共{$Page->totalPages}页，第{$Page->nowPage}页：\n\n\n" . $rule_reply;
					if ($Page->totalPages > $next_page) {
						$rule_reply .= "回复“搜{$k}{$next_page}”看第{$next_page}页，如果关键词末尾是数字，在页码前加空格。";
					}

				}
				/******回复带搜****/
			} else {
			//	$this->wechatObj->makeText($Content);
				$this->makeNews(trim($Content));
			}
			$rule_reply = str_replace("<br />", "\n", $rule_reply);
			$rule_reply = strip_tags($rule_reply, "<a>");
			$rule_reply = str_replace("&nbsp;", " ", $rule_reply);
			if ($rule_reply) {
				$this->wechatObj->makeText($rule_reply);
			}

		}
		/********关键字回复结束***/

	}

}
